package com.heima.article.listen;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.PublishArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PublishArticleListener {
    @Autowired
    ApArticleService apArticleService;

    @RabbitListener(queuesToDeclare = @Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE))
    public void handlePublishArticle(String newsId) {
        log.info("接收到 发布文章消息  文章id : {}", newsId);
        try {
            apArticleService.publishArticle(Integer.valueOf(newsId));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("文章发布失败! 出现异常 文章的id为: {}", newsId);
        }
    }
}
